package org.niu.str;

public class ShortestCompletingWord {
    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] cnt = new int[26];
        for (int i = 0; i < licensePlate.length(); i++) {
            char ch = licensePlate.charAt(i);
            if(Character.isLetter(ch)){
                cnt[Character.toLowerCase(ch)-'a']++;
            }
        }
        int idx = -1;
        for (int i = 0; i < words.length; i++) {
            int[] c = new int[26];
            for (int j = 0; j < words[i].length(); j++) {
                char ch = words[i].charAt(j);
                c[ch-'a']++;
            }
            boolean flag = false;
            for (int j = 0; j < 26; j++) {
                if(c[j]<cnt[j]){
                    flag = true;
                    break;
                }
            }
            if(flag && (idx<0||words[i].length()<words[idx].length())){
                // 更新index
                idx = i;
            }
        }
        return  words[idx];

    }
}
